<template>
	<view class="container">
		<view>
			<view class="first-header">
				<view>
					<image src="/static/logo.png"></image>
				</view>
			</view>
			<view class="first-login">
				<text>请完成登陆以继续使用</text>
				<view>
					<button open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGetUserInfo" v-if="mode==0">微信用户快捷登陆</button>
					<button open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="onGetPhone" v-else>手机号授权</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp();
	import http from '../../common/js/request.js';
	export default {
		data() {
			return {
				mode: 0,
				url: app.globalData.baseImgUrl
			}
		},
		onLoad() {

		},
		methods: {

			//获取用户信息
			onGetUserInfo: function(e) {
				var that = this;
				uni.showLoading();
				var userInfo = e.detail.userInfo;
				if (!userInfo) {
					uni.hideLoading();
					uni.showToast({
						title: '拒绝了授权~',
						icon: 'none'
					});
				}
				http.post("/api/v1/member/member_modify", {
					avatar: userInfo.avatarUrl,
					nickname: userInfo.nickName,
					sex: userInfo.gender,
					phone: "",
					country: userInfo.country,
					province: userInfo.province,
					city: userInfo.city,
				}, (res) => {
					uni.hideLoading();
					if (res.state == 1) {
						that.mode = 1;
					} else {
						uni.showToast({
							title: res.datas,
							icon: 'none'
						});
					}
				});
			},
			onGetPhone: function(e) {
				var that = this;
				if (!e.detail.iv || !e.detail.encryptedData) {
					uni.showToast({
						title: '授权手机号失败',
						icon: 'none'
					});
					return;
				}

				uni.login({
					success: function(res) {
						http.post('/api/v1/common/get_wechat_phone', {
							// encodeURIComponent
							iv: e.detail.iv,
							encryptedData: e.detail.encryptedData,
							js_code: res.code
						}, res => {
							if (res.state != 1) {
								uni.showToast({
									title: '授权手机号失败',
									icon: 'none'
								});
								return;
							}
							uni.setStorageSync('phone', res.datas)
							http.post("/api/v1/member/member_modify", {
								phone: res.datas,
							}, (res) => {
								if (res.state == 1) {
									uni.showToast({
										title: '登录成功~',
										success: function() {
											setTimeout(function() {
												that.redirect()
											}, 500)
										}
									});
								}
							});
						})
					}
				});
			},
			//跳转至设置的缓存页面
			redirect: function() {
				var path = uni.getStorageSync('path')
				console.log('获取的路径缓存为', path)
				uni.redirectTo({
					url: path,
					success: function() {
						uni.hideNavigationBarLoading();
						uni.removeStorage({
							key: 'path',
						})
					},
					fail: function() {
						uni.switchTab({
							url: path,
							success: function(res) {
								uni.hideNavigationBarLoading();
								uni.removeStorage({
									key: 'path',
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	/* pages/firstPage/firstPage.wxss */

	.first-header {
		margin: 100rpx auto;
	}

	.first-header text {
		display: block;
		text-align: center;
		font-size: 30rpx;
		line-height: 88rpx;
	}

	.first-header view {
		/* box-shadow:0px 8rpx 20rpx 2rpx rgba(0, 0, 0, 0.14);
	border-radius:10rpx; */
		width: 388rpx;
		height: 344rpx;
		overflow: hidden;
		margin: 0 auto;
	}

	.first-header view image {
		width: 100%;
		height: 100%;
	}

	.first-login {
		width: 80%;
		margin: 0 auto;
	}

	.first-login text {
		text-align: center;
		display: block;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 26rpx;
		color: #727070;
	}

	.first-login>view button {
		height: 88rpx;
		margin-bottom: 38rpx;
		font-size: 30rpx;
		line-height: 88rpx;
		border: 1px solid #FF9D11;
		background: #fff;
	}

	.first-login>view button:nth-child(1) {
		background: #FF9D11 !important;
		color: #fff !important;
	}
</style>
